<template>
    <div>

        <div v-show="ad==1">
            <el-form :inline="true" :model="sendwaay" class="demo-form-inline">
                <el-form-item >
                    <el-input v-model="sendwaay.sendName" size="small" placeholder="公司类型"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="getall" size="small" >查询</el-button>
                    <el-button type="primary" @click="GetsendName" size="small" >添加</el-button>

                </el-form-item>
            </el-form>
            <el-table :data="sendwaays" height="200" border style="width: 100%">
                <el-table-column label="公司类型" prop="sendName"></el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-dropdown>
                            <el-button type="primary" size="small" >
                                操作<i class="el-icon-arrow-down el-icon--right"></i>
                            </el-button>
                            <el-dropdown-menu slot="dropdown">
                                <el-dropdown-item size="small"  @click.native="UpsendName(scope)">修改</el-dropdown-item>
                                <el-dropdown-item size="small" @click.native="del(scope)">删除</el-dropdown-item>
                            </el-dropdown-menu>
                        </el-dropdown>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div v-if="ad==2">
            <el-form :model="sendw" :rules="rules"
                     ref="recordForm"
                     class="demo-ruleForm">
                <el-form-item label="公司类型：" prop="uname">
                    <el-input v-model="sendw.sendName" size="small" placeholder="公司类型"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="addsendwaay('recordForm')" :disabled="disable" size="small" >提交</el-button>
                    <el-button type="primary" @click="coles" size="small" >取消</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    import sendwaay from "../../../model/gsl/SendWaay/sendwaay";
    const send=sendwaay.getInstance();
    export default {
        name: "sendwaay",
        data(){
            var uname = (rule, value, callback) => {
                value=this.sendw.sendName;
                if (!value) {
                    this.disable=true;
                    callback(new Error('快件类型名称'));
                } else {
                    callback();
                    this.disable=false;
                }
            };
            return{
                sendwaays:[],
                sendwaay:{},
                sendw:{},
                ad:1,
                disable:true,
                rules: {
                    uname: [{ required: true,validator: uname, trigger: 'blur' }],
                }
            }
        },
        created(){
            this.getall();
        },
        methods: {
            getall(){
                send.list(this.sendwaay).then((re)=>{
                    this.sendwaays=re.data.data;
                })
            },
            del(so){
                send.remove(so.row.id).then((re)=>{
                    if (re.data.data==1) {
                        alert("删除成功！");
                        this.getall();
                    }else {
                        alert("删除失败！")
                        this.getall();
                    }
                })
            },
            GetsendName(){
                this.sendw={}
                this.ad=2;
            },
            UpsendName(ss){
                this.ad=2;
                send.listByid(ss.row.id).then((re)=>{
                    this.sendw={}
                    this.sendw=re.data.data;
                })
            },
            coles(){
                this.ad=1;
            },
            addsendwaay(formName){
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        console.log(this.sendw);
                        if (!this.sendw.id) {
                            send.save(this.sendw).then((re)=>{
                                if (re.data.data==1) {
                                    alert("添加成功！");
                                    this.getall();
                                    this.ad=1;
                                }else {
                                    alert("添加失败！")
                                    this.ad=2;
                                }
                            })
                        }else {
                            send.update(this.sendw).then((re)=>{
                                if (re.data.data==1) {
                                    alert("修改成功！");
                                    this.getall();
                                    this.ad=1;
                                }else {
                                    alert("修改失败！")
                                    this.ad=2;
                                }
                            })
                        }
                        this.sendw={};
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });

            }
        }
    }
</script>

<style scoped>

</style>